package com.py.credit.service.impl;

import com.alibaba.fastjson.JSON;
import com.py.credit.dao.UserReportDao;
import com.py.credit.service.UserReportService;
import com.py.credit.service.XypjRemoteBaseService;
import com.py.credit.utils.UuidUtil;
import org.apache.commons.net.ntp.TimeStamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Company: WWW.xypy.com.cn
 * @ClassName UserReportServiceImpl
 * @Description: 用户申报管理前台业务接口实现类
 * @Params: Test
 * @Author: YaoS
 * @Create: 2018-10-22 15:28
 **/
@Service
public class UserReportServiceImpl implements UserReportService {

    /**
     * 判断cube类型
     **/
    private static final String INSERT = "I";
    /**
     * 判断finance类型
     **/
    private static final String UPDATE = "U";
    @Autowired
    UserReportDao userReportDao;
    @Autowired
    XypjRemoteBaseService xypjRemoteBaseService;

    @Override
    public String selectLeftTable(String rootId) {
        return userReportDao.selectCubeTable(rootId);
    }

    @Override
    public List<Map<String, Object>> selectRecordList(String tybm, String tableId) {
        List list = new ArrayList();
        //根据表id和企业统一编码查询当前企业的record和详情数据
        List<Map<String, Object>> recordList = userReportDao.selectRecordList(tybm, tableId);
        for (Map<String, Object> map : recordList) {
            String recordId = (String) map.get("id");
            List<Map<String, Object>> recordValues = userReportDao.selectRecordDetail(recordId);
            map.put("detail", recordValues);
            list.add(map);
        }
        return list;
    }

    @Override
    public void saveTableForRecord(String uuid, String tybm, String type, String tableName, String tableId, String dataId, String name) {
        userReportDao.insertRecordTable(uuid, tableName, tableId, tybm, type, dataId, name);
    }

    @Override
    public void saveTableForRecordDetail(String key, String value, String uuid, String codeName) {
        userReportDao.insertRecordDetail(key, value, uuid, codeName);
    }

    @Override
    public void updateRecordByStatus(String id, String isDel) {
        userReportDao.updateRecordByStatus(id, isDel);
    }

    @Override
    public void updateRecordDetail(String key, String value, String recordId) {
        userReportDao.updateRecordDetail(key, value, recordId);
    }

    @Override
    public List<Map<String, Object>> selectRecordDetail(String id) {
        return userReportDao.selectRecordDetail(id);
    }

    @Override
    public void commitTableList(String tybm, String tableId, String cjr, String ztmc, String ztlb) {
        String processId = "";
        String detailId = "";

        //查询所有待提交数据
        List<Map<String, Object>> list = userReportDao.selectCommitRecordList(tybm, tableId);
        for (Map<String, Object> map : list) {
            processId = UuidUtil.getUUid();
            //插入xypj_data_collection_process表
            userReportDao.addProcess(processId, (String) map.get("tableName"), (String) map.get("chinaName"), (String) map.get("tableId"), ztmc, cjr, (String) map.get("status"), ztlb, (String) map.get("dataId"), (String) map.get("id"));
            //查询对应t_record_detail表数据
            List<Map<String, Object>> cloumnList = userReportDao.selectRecordDetail((String) map.get("id"));
            //循环插入xypj_data_collection_detail表
            //TODO 建议不要循环插入，应当处理好数据后单条插入
            if ("I".equals(map.get("status"))) {
                for (Map<String, Object> cloumnMap : cloumnList) {
                    detailId = UuidUtil.getUUid();
                    userReportDao.addDetail(detailId, (String) cloumnMap.get("valueName"), (String) map.get("tableName"), (String) cloumnMap.get("codeName"), cjr, processId, (String) cloumnMap.get("keyName"), null);
                }
            } else {
                LinkedHashMap oldData = xypjRemoteBaseService.getTableInfoById((String) map.get("tableName"), (String) map.get("dataId"), "").getData();
                for (Map<String, Object> cloumnMap : cloumnList) {
                    detailId = UuidUtil.getUUid();
                    Object object = oldData.get(cloumnMap.get("keyName"));
                    String oldValue = "";
                    if (object!=null) {
                        if (object instanceof TimeStamp) {
                            oldValue = (String) oldData.get(cloumnMap.get("keyName"));
                        } else {
                            oldValue = oldData.get(cloumnMap.get("keyName")).toString();
                        }
                    }
                    userReportDao.addDetail(detailId, (String) cloumnMap.get("valueName"), (String) map.get("tableName"), (String) cloumnMap.get("codeName"), cjr, processId, (String) cloumnMap.get("keyName"), oldValue);
                }
            }


        }
        //将当前用户的t_record表审核状态改为D，表示待提交
        userReportDao.updateRecordList("D", tybm, tableId);
    }

    @Override
    public void delRecord(String id) {
        userReportDao.delRecordDetail(id);
        userReportDao.delRecord(id);
    }

    @Override
    public void editorRecord(String recordId, String dataList) {
        userReportDao.delRecordDetail(recordId);
        String key = "";
        String value = "";
        String codeName = "";
        //将详细数据计入recordDetail表
        ArrayList<HashMap<String, Object>> list = JSON.parseObject(dataList, ArrayList.class);
        for (Map map : list) {
            key = (String) map.get("key");
            value = String.valueOf(map.get("value"));
            codeName = (String) map.get("codeName");
            userReportDao.insertRecordDetail(key, value, recordId, codeName);
        }
    }

    @Override
    public int checkTableAudit(String tybm, String resourceId) {
        return userReportDao.checkTableAudit(tybm, resourceId);
    }

}
